package com.ding.easycontroller.config;

import com.ding.easycontroller.socket.ConsoleWebSocketHandler;
import com.ding.easycontroller.socket.DispatcherWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private DispatcherWebSocketHandler dispatcherWebSocketHandler;

    @Autowired
    private ConsoleWebSocketHandler consoleWebSocketHandler;

    @Value("${websocket.basePath:/socket/}")
    private String base;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        base = base.trim();
        if (base.equals("/config/")) {
            throw new RuntimeException("websocket基础路径不能为:" + base);
        }
        webSocketHandlerRegistry.addHandler(dispatcherWebSocketHandler, base + "**");
        webSocketHandlerRegistry.addHandler(consoleWebSocketHandler, "/_console");
    }


}
